package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceCode;
import cn.tedu.csmall.product.ex.ServiceException;
import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    /**
     * 业务状态码
     */
    private Integer state;
    /**
     * 提示消息
     */
    private String message;

    /**
     * 成功时响应的请求
     */
    private T data;

    public JsonResult() {
    }

    private JsonResult(Integer state, String message,T data) {
        this.state = state;
        this.message = message;
        this.data=data;
    }

    public static JsonResult<Void> ok(){
        /*return new JsonResult(ServiceCode.OK.getValue(), null,null);*/
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        return new JsonResult(ServiceCode.OK.getValue(),null,data);
    }

    public static JsonResult<Void> fail(Integer state,String message){
        return new JsonResult(state,message,null);
    }

    public static JsonResult<Void> fail(ServiceException e){
       /* return fail(e.getServiceCode().getValue(),e.getMessage(),null);*/
        return fail(e.getServiceCode().getValue(),e.getMessage());
    }
}
